package com.ssm.service.impl;
import java.util.List;

import com.ssm.pojo.Book;
import com.ssm.pojo.Guan;
import com.ssm.pojo.Record;
import com.ssm.pojo.Yong;
import com.ssm.service.GuanService;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ssm.pojo.Page;
import com.ssm.li.PageConstant;
import com.ssm.mapper.Guanmapper;


@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class GuanServiceImpl implements GuanService {
    @Autowired
    private Guanmapper guanmapper;

    @Override
    public Page getRecord(Record records) {
        // 开始
        Page page = new Page();
        if (records.getCurrentPage() == 0) {
            records.setCurrentPage(PageConstant.PAGE_NUM);
        }
        int start = (records.getCurrentPage() - 1) * PageConstant.PAGE_SIZE;
        records.setStart(start);
        List<Record> list = guanmapper.getRecord(records);
        page.setList(list);
        int count = guanmapper.getCount(records);
        int counPage = count / PageConstant.PAGE_SIZE;
        if (count % PageConstant.PAGE_SIZE != 0) {
            counPage++;
        }
        // 总页数
        page.setTotalPage(counPage);
        // 总记录数
        page.setCount(count);
        return page;
    }

    @Override
    public void updateRecord(long id){
        // TODO Auto-generated method stub
        guanmapper.updateRecord(id);
    }

    @Override
    public void notAllowRecord(long id){
        //  TODO Auto-generated method stub
        guanmapper.notAllowRecord(id);
    }

    @Override
    public  void insertBackBooks(long id){
        // TODO Auto-generated method stub
        guanmapper.insertBackBooks(id);
    }

    @Override
    //这里的是records
    public Page getBackBooks(Record records) {
        Page page = new Page();
        if (records.getCurrentPage() == 0) {
            records.setCurrentPage(PageConstant.PAGE_NUM);
        }
        int start = (records.getCurrentPage() - 1) * PageConstant.PAGE_SIZE;
        records.setStart(start);
        List<Record> list = guanmapper.getBackBooks(records);
        page.setList(list);
        int count = guanmapper.getCount2(records);
        int counPage = count / PageConstant.PAGE_SIZE;
        if (count % PageConstant.PAGE_SIZE != 0) {
            counPage++;
        }
        // 总页数
        page.setTotalPage(counPage);
        // 总记录数
        page.setCount(count);
        return page;
    }
    @Override
    public void allowBackBook(long id) {
        // TODO Auto-generated method stub
        guanmapper.allowBackBook(id);
    }


    @Override
    //???
    public Guan getGuan(Yong yong) {
        // TODO Auto-generated method stub
        return guanmapper.getGuan(yong);
    }

    @Override
    public int selectRecordId(long id) {
        // TODO Auto-generated method stub
        return guanmapper.selectRecordId(id);
    }

    @Override
    public int selectBooksId(long id) {
        // TODO Auto-generated method stub
        return guanmapper.selectBooksId(id);
    }

    @Override
    public void updateBooks(long id) {
        // TODO Auto-generated method stub
        guanmapper.updateBooks(id);
    }

    @Override
    public void descBooks(long id) {
        // TODO Auto-generated method stub
        guanmapper.descBooks(id);
    }

    @Override
    public void updateRecordTime(Record record) {
        // TODO Auto-generated method stub
        guanmapper.updateRecordTime(record);
    }

    @Override
    public Page getBooks(Book book) {
        Page page = new Page();
        if (book.getCurrentPage() == 0) {
            book.setCurrentPage(PageConstant.PAGE_NUM);
        }
        int start = (book.getCurrentPage() - 1) * PageConstant.PAGE_SIZE;
        book.setStart(start);
        List<Book> list = guanmapper.getBooks(book);
        page.setList(list);
        int count = guanmapper.getCount(book);
        int counPage = count / PageConstant.PAGE_SIZE;
        if (count % PageConstant.PAGE_SIZE != 0) {
            counPage++;
        }
        // 总页数
        page.setTotalPage(counPage);
        // 总记录数
        page.setCount(count);
        return page;
    }

    @Override
    public void addBooks(Book book) {
        guanmapper.addBooks(book);
    }

    @Override
    public Book getBookById(long id) {
        return guanmapper.getBookById(id);
    }

    @Override
    public void updateBooks(Book book) {
        guanmapper.updateBooks(book);
    }

    @Override
    public void deleteBooks(long id) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        guanmapper.deleteBooks(id);
    }

    @Override
    public List<Yong> getYong(Yong yong) {
        return guanmapper.getYong(yong);
    }

    @Override
    public void addYong(Yong yong) {
       guanmapper.addYong(yong);

    }

    @Override
    public Yong getYongById(long id) {
        return guanmapper.getYongById(id);
    }

    @Override
    public void updateYong(Yong yong) {
        guanmapper.updateYong(yong);
    }

    @Override
    public void deleteYong(long id) {
        guanmapper.deleteYong(id);
    }

    @Override
    public int selectYong(String name) {
        return guanmapper.selectYong(name);
    }
}
